/******************************************************************************* 
 * Copyright (c) 2008 xored software, Inc.  
 * 
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution, and is available at 
 * http://www.eclipse.org/legal/epl-v10.html  
 * 
 * Contributors: 
 *     xored software, Inc. - initial API and Implementation (Yuri Strot) 
 *******************************************************************************/
package com.xored.glance.internal.core;

import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.graphics.Image;

import com.xored.glance.core.StyleList;
import com.xored.glance.search.SearchUnit;

/**
 * @author Yuri Strot
 * 
 */
public class DecoratedText {

	private String text;

	private SearchUnit unit;
	private int start;
	private int end;
	private StyleList styles;

	public DecoratedText(SearchUnit unit, int start, int end) {
		this.unit = unit;
		this.start = start;
		this.end = end;
	}

	/**
	 * @return the image
	 */
	public Image getImage() {
		return unit.getContainer().getImage();
	}

	/**
	 * @return the string
	 */
	public String getText() {
		if (text == null) {
			text = unit.getText();
			text = text.substring(start, end);
		}
		return text;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof DecoratedText) {
			DecoratedText dt = (DecoratedText) obj;
			return unit.equals(dt.unit) && start == dt.start && end == dt.end;
		}
		return false;
	}

	/**
	 * @return the styles
	 */
	public StyleList getStyles() {
		if (styles == null) {
			styles = unit.getContainer().getStyles().subStyles(start, end, start);
		}
		return styles;
	}

	public void addStyle(StyleRange range) {
		range.start -= start;
		styles = getStyles().applyStyles(new StyleList(range));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return unit.hashCode() ^ start ^ end;
	}

}
